/*
 * Configuration.java
 *
 * Created on 23 janvier 2007, 18:58
 *
 * Copyright (C) 2006-2007 Alexis ROBERT
 * Under the terms of the GPL2 license.
 */

package blbarc;

import java.util.HashMap;
import java.util.Locale;
import org.jvyaml.YAML;
import java.util.Map;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.Writer;
import java.io.IOException;

/**
 * Configuration abstraction singleton (YAML implementation)
 * @author Alexis ROBERT
 */
public class Configuration {
   private Map<Object, Object> configdata;
   
   /**
    * Get the Configuration instance
    * @return Instancied configuration object
    */
   public static Configuration getInstance() {
        if (instance == null) {
            instance = new Configuration();
        }
        return instance;
   }

   private Configuration() {
       configdata = null;
       try {
            configdata = (Map)YAML.load(new FileReader("config.yml"));
       } catch (FileNotFoundException ex) {
            System.out.println("Creating config.yml ...");
            configdata = new HashMap<Object,Object>();
       }
   }
   
   /**
    * Get the BCDB server address
    * @return BCDB server address
    */
   public String getBCDBServer() {
       return get("BCDBServer", "http://bcdb.robertlan.eu.org/RPC2");
   }
   
   /**
    * Set the BCDB server address
    * @param val BCDB server address
    */
   public void setBCDBServer(String val) {
       configdata.put("BCDBServer", val);
       save();
   }
   
   /**
    * Get the EAN13 check digit property
    * @return true if he wants, false otherwise
    */
   public boolean getEAN13CheckDigit() {
       return get("EAN13CheckDigit", true);
   }
   
   /**
    * Sets the EAN13 check digit property
    * @param val EAN13 check digit property to store
    */
   public void setEAN13CheckDigit(Boolean val) {
       configdata.put("EAN13CheckDigit", val);
       save();
   }
   
   /**
    * Get the internet access property
    * @return true if he wants, false otherwise
    */
   public boolean getInternet() {
       return get("internet", true);
   }
   
   /**
    * Set the internet access property
    * @param val Internet access property to store
    */
   public void setInternet(Boolean val) {
       configdata.put("internet", val);
       save();
   }
   
   /**
    * Get the language property
    * @return language string containing language and country
    */
   public String getLanguage() {
       return get("language", Locale.getDefault().toString()).toLowerCase();
   }
   
   /** Set the language property
    * @param val Language property to store
    */
   public void setLanguage(String val) {
       configdata.put("language", val);
       save();
   }
   
   /**
    * Get an attribute in the configuration file (boolean values)
    * @param name Attribute name
    * @param defaultValue If this attribute doesn't exist, put this as the value
    * @return boolean value
    */
   private boolean get(String name, boolean defaultValue) {
       Boolean b = (Boolean)(get(name, (Object)defaultValue));
       if (b == b.TRUE) // TODO: Find a better way
           return true;
       else
           return false;
   }
   
   /**
    * Get an attribute in the configuration file (String values)
    * @param name Attribute name
    * @param defaultValue If this attribute doesn't exist, put this as the value
    * @return String value
    */
   private String get(String name, String defaultValue) {
       return (String)get(name,(Object)defaultValue);
   }
   
   /**
    * Get an attribute in the configuration file (Object values)
    * @param name Attribute name
    * @param defaultValue If this attribute doesn't exist, put this as the value
    * @return Object value
    */
   private Object get(String name, Object defaultValue) {
       Object obj = configdata.get(name);
       if (obj == null) {
           configdata.put(name, defaultValue);
           save();
           return defaultValue;
       } else {
           return obj;
       }
   }
   
   /** Save configuration */
   public void save() {
       if (configdata != null) {
           try {
               YAML.dump(configdata, (Writer)(new FileWriter("config.yml")));
           } catch (IOException ex) {
               ex.printStackTrace();
           }
       }
   }

   private static Configuration instance;
}
